/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cadi.games;

import com.cadi.aux.Coin;
import com.cadi.aux.Window;
import com.cadi.menu.MainMenu;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;

/**
 *
 * @author ApoZuniga
 */
public class SuperDifficultLevel extends Window {

    private JLabel instruction;
    private JLabel totalQuestion;
    private JLabel problem;
    private JLabel superImage;
    private JLabel itemOne;
    private JLabel itemTwo;
    private JLabel itemThree;
    private JLabel textTotal;
    private JLabel labelChange;
    private JLabel yourChange;
    protected Coin billChange;
    protected TextField tfTotal;
    protected TextField tfChange;
    private JButton submitTotal;
    private JButton checkChange;
    protected int valueTotal;
    protected int valueChange;
    protected int currentLevel;
    private String bill;
    private int vBill;

    public SuperDifficultLevel() {
        super();
        voice = java.applet.Applet.newAudioClip(getClass().getResource("/resources/resuelve.wav"));
        voice.play();
        character.setBounds(5, 440, 149, 300);
        playVoice.setBounds(135, 550, 50, 50);
        instruction = new JLabel("Da la solución correcta al siguiente problema");
        instruction.setBounds(50, 10, 500, 20);
        instruction.setFont(styleText);
        totalQuestion = new JLabel("¿Cuánto fue el total que pagó?");
        totalQuestion.setFont(styleText);
        totalQuestion.setBounds(230,  500, 500, 30);
        problem = new JLabel(new ImageIcon(getClass().getResource("/resources/problem1.png")));
        problem.setBounds(50, 50, 978, 150);
        textTotal = new JLabel("Total a pagar: ");
        textTotal.setFont(styleText);
        textTotal.setBounds(230, 550, 160, 30);
        tfTotal = new TextField();
        tfTotal.setBounds(380, 550, 80, 30);
        submitTotal = new JButton("Enviar Resultado");
        submitTotal.setBounds(270, 600, 150, 30);
        labelChange = new JLabel();
        labelChange.setFont(styleText);
        labelChange.setBounds(650, 480, 500, 30);
        billChange = new Coin(200, "large");
        billChange.setBounds(790, 530, 150, 70 );
        yourChange = new JLabel();
        yourChange.setFont(styleText);
        yourChange.setBounds(700, 630, 130, 30);
        tfChange = new TextField();
        tfChange.setBounds(840, 630, 80, 30);
        checkChange = new JButton("Revisar Cambio");
        checkChange.setBounds(940, 630, 150, 30);
        currentLevel = 1;
        bill = "200";
        vBill = 200;
        
        itemOne = new JLabel(new ImageIcon(getClass().getResource("/resources/compra3_5.png")));
        itemOne.setBounds(260, 150, 135, 302);
        itemTwo = new JLabel(new ImageIcon(getClass().getResource("/resources/compra3_6.png")));
        itemTwo.setBounds(570, 150, 140, 302);
        itemThree = new JLabel(new ImageIcon(getClass().getResource("/resources/compra3_1.png")));
        itemThree.setBounds(885, 150, 142, 302);
        valueTotal = 120;
        valueChange = 80;

        container.add(instruction, JLayeredPane.PALETTE_LAYER);
        container.add(totalQuestion, JLayeredPane.PALETTE_LAYER);
        container.add(problem, JLayeredPane.PALETTE_LAYER);
        container.add(labelChange, JLayeredPane.PALETTE_LAYER);
        container.add(billChange, JLayeredPane.PALETTE_LAYER);
        container.add(yourChange, JLayeredPane.PALETTE_LAYER);
        container.add(itemOne, JLayeredPane.PALETTE_LAYER);
        container.add(itemTwo, JLayeredPane.PALETTE_LAYER);
        container.add(itemThree, JLayeredPane.PALETTE_LAYER);
        container.add(tfTotal, JLayeredPane.PALETTE_LAYER);
        container.add(tfChange, JLayeredPane.PALETTE_LAYER);
        container.add(checkChange, JLayeredPane.PALETTE_LAYER);
        container.add(textTotal, JLayeredPane.PALETTE_LAYER);
        container.add(submitTotal, JLayeredPane.PALETTE_LAYER);
        superImage = new JLabel(new ImageIcon(getClass().getResource("/resources/fondo4.png")));
        superImage.setBounds(0, 0, 1280, 800);
        container.add(superImage, JLayeredPane.PALETTE_LAYER);
        billChange.setVisible(false);
        tfChange.setVisible(false);
        checkChange.setVisible(false);
        submitTotal.addActionListener(new ActionListener() {
        
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int userValue = Integer.parseInt(tfTotal.getText().trim());
                    if (userValue == valueTotal) { 
                        result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/bien.wav"));
                        result.play();
                        JOptionPane.showMessageDialog(null, "¡Bien Hecho!", "¡Bien Hecho!", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/right.png")));
                        showChange(bill, vBill);
                    } else {
                        result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
                        result.play();
                        JOptionPane.showMessageDialog(null, "Intenta de nuevo", "Intenta de nuevo", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/wrong.png")));
                    }
                } catch (Exception ex) {
                    result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
                     result.play();
                    JOptionPane.showMessageDialog(null, "Ingresaste algo inválido", "Ingresaste algo inválido", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/wrong.png")));
                } 
                window.repaint();
            }
        });
        
        checkChange.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int userValue = Integer.parseInt(tfChange.getText().trim());
                    if (userValue == valueChange) {
                        result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/bien.wav"));
                        result.play();
                        JOptionPane.showMessageDialog(null, "¡Bien Hecho!", "¡Bien Hecho!", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/right.png")));
                        changeLevel();
                    }  else {
                        result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
                        result.play();
                        JOptionPane.showMessageDialog(null, "Intenta de nuevo", "Intenta de nuevo", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/wrong.png")));
                    }
                } catch (Exception ex) {
                    result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
                    result.play();
                    JOptionPane.showMessageDialog(null, "Ingresaste algo inválido", "Ingresaste algo inválido", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/wrong.png")));
                }
            }
        });
        window.setVisible(true);
    }
    
    
    public void showChange (String bill, int vBill) {
        labelChange.setText("Si pagas con un billete de $ "+ bill + ",¿Cuál será tu cambio?");
        billChange.changeIcon(vBill);
        billChange.setVisible(true);
        yourChange.setText("El cambio es: ");
        tfChange.setVisible(true);
        checkChange.setVisible(true);
        labelChange.setVisible(true);
        yourChange.setVisible(true);
        window.repaint();
    }
    
    private void changeLevel() {
        switch(currentLevel){
            case 1:
                setLevelTwo();
                currentLevel++;
                break;
            case 2:
                setLevelThree();
                currentLevel++;
                break;
            case 3:
                setLevelFour();
                currentLevel++;
                break;
            case 4:
                setLevelFive();
                currentLevel++;
                break;
            case 5:
                MainMenu main = new MainMenu();
                window.setVisible(false);
                break;
        }
    }
    
    private void setLevelTwo() {
        billChange.setVisible(false);
        tfChange.setVisible(false);
        checkChange.setVisible(false);
        labelChange.setVisible(false);
        yourChange.setVisible(false);
        problem.setIcon(new ImageIcon(getClass().getResource("/resources/problem2.png")));
        itemOne.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_2.png")));
        itemTwo.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_1.png")));
        itemThree.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_8.png")));
        valueTotal = 83;
        valueChange = 17;
        bill = "100";
        vBill = 100;
        tfChange.setText(" ");
        tfTotal.setText(" ");
        window.repaint();
        voice.play();
    }
    
    private void setLevelThree() {
        billChange.setVisible(false);
        tfChange.setVisible(false);
        checkChange.setVisible(false);
        labelChange.setVisible(false);
        yourChange.setVisible(false);
        problem.setIcon(new ImageIcon(getClass().getResource("/resources/problem3.png")));
        itemOne.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_1.png")));
        itemTwo.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_4.png")));
        itemThree.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_4.png")));
        valueTotal = 65;
        valueChange = 135;
        bill = "200";
        vBill = 200;
        tfChange.setText(" ");
        tfTotal.setText(" ");
        window.repaint();
        voice.play();
    }
    
    private void setLevelFour() {
        billChange.setVisible(false);
        tfChange.setVisible(false);
        checkChange.setVisible(false);
        labelChange.setVisible(false);
        yourChange.setVisible(false);
        problem.setIcon(new ImageIcon(getClass().getResource("/resources/problem4.png")));
        itemOne.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_6.png")));
        itemTwo.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_8.png")));
        itemThree.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_9.png")));
        valueTotal = 78;
        valueChange = 422;
        bill = "500";
        vBill = 500;
        tfChange.setText(" ");
        tfTotal.setText(" ");
        window.repaint();
        voice.play();
    }
    
    private void setLevelFive() {
        billChange.setVisible(false);
        tfChange.setVisible(false);
        checkChange.setVisible(false);
        labelChange.setVisible(false);
        yourChange.setVisible(false);
        problem.setIcon(new ImageIcon(getClass().getResource("/resources/problem5.png")));
        itemOne.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_4.png")));
        itemTwo.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_6.png")));
        itemThree.setIcon(new ImageIcon(getClass().getResource("/resources/compra3_8.png")));
        valueTotal = 76;
        valueChange = 424;
        bill = "500";
        vBill = 500;
        tfChange.setText(" ");
        tfTotal.setText(" ");
        voice.play();
        window.repaint();
    }
}
